package com.example.jh352160.surfingtv.mypage.childPage;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;

import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.category.customView.Subscribe_item;
import com.example.jh352160.surfingtv.category.customView.adapter.SubscribeListAdapter;
import com.example.jh352160.surfingtv.mypage.customView.adapter.CommodityRecyclerAdapter;
import com.example.jh352160.surfingtv.mypage.customView.adapter.FavoritesPagerAdapter;
import com.example.jh352160.surfingtv.network.item.MyPageFavoriteCommonityItem;
import com.example.jh352160.surfingtv.network.item.MyPageFavoriteThematicItem;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.MyPageFavoriteCommonityService;
import com.example.jh352160.surfingtv.network.service.MyPageFavoriteThematicService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by jh352160 on 2016/7/27
 */

public class Favorites extends AppCompatActivity {

    private static final int THEMATIC_OK = 0;
    private static final int COMMODITY_OK = 1;
    Toolbar toolbar;
    ViewPager viewPager;
    List<View> viewList;
    TabLayout tabLayout;
    List<Subscribe_item> thematic_items;
    List<String> titleList, imageUrlList, aboutList, detailUrlList;
    List<Integer> commpdityContentIdList, favoriteCountList;
    List<Boolean> isFavoriteList;

    List<String> commonityName, commonityImageUrl, commonityFrom, commonityDetilUrl;
    List<Boolean> commonityIsLike;
    List<Integer> commonityId;
    int finishPage;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case THEMATIC_OK:
                    thematicInit();
                    conectCommonityNetWork();
                    break;
                case COMMODITY_OK:
                    commodityInit();
                    break;
            }
            if (finishPage == 2) {
                pageFinish();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mypage_favorites);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("");
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewList = new ArrayList<>();
        //init();
        conectThematicNetWork();
//        FavoritesPagerAdapter adapter=new FavoritesPagerAdapter(viewList);
//        adapter.notifyDataSetChanged();
//        viewPager.setAdapter(adapter);
//        tabLayout.addTab(tabLayout.newTab().setText("收藏"));
//        tabLayout.addTab(tabLayout.newTab().setText("商品"));
//        tabLayout.setupWithViewPager(viewPager);


    }

//    private void init() {
//        String[] titles = {"专题1", "专题2", "专题3", "专题4"};
//        String[] abouts = {"简介1", "简介2", "简介3", "简介4"};
//        List<Subscribe_item> subscribe_items = new ArrayList<>();
//        for (int i = 0; i < titles.length; i++) {
//            Subscribe_item subscribe_item = new Subscribe_item(this);
//            subscribe_item.setText(titles[i], abouts[i]);
//            subscribe_items.add(subscribe_item);
//        }
//        SubscribeListAdapter adapter = new SubscribeListAdapter(this, R.layout.category_subscribe_item, subscribe_items);
//        ListView listView = new ListView(this);
//        listView.setAdapter(adapter);
//        viewList = new ArrayList<>();
//        viewList.add(listView);
//
//        RecyclerView commodityView = new RecyclerView(this);
//        CommodityRecyclerAdapter adapter1 = new CommodityRecyclerAdapter();
//        commodityView.setLayoutManager(new GridLayoutManager(this, 3));
//        commodityView.setAdapter(adapter1);
//        viewList.add(commodityView);
//    }

    private void conectThematicNetWork() {
        //专题收藏部分api
        Retrofit retrofit = CommonRetrofit.getRetrofit();
        MyPageFavoriteThematicService myPageFavoriteThematicService = retrofit.create(MyPageFavoriteThematicService.class);
        Call<MyPageFavoriteThematicItem> thematicServiceCall = myPageFavoriteThematicService.getItem(3);
        thematicServiceCall.enqueue(new Callback<MyPageFavoriteThematicItem>() {
            @Override
            public void onResponse(Call<MyPageFavoriteThematicItem> call, Response<MyPageFavoriteThematicItem> response) {
                List<MyPageFavoriteThematicItem.DataBean.ItemsBean> items = response.body().getData().getItems();
                titleList = new ArrayList<>();
                imageUrlList = new ArrayList<>();
                aboutList = new ArrayList<>();
                detailUrlList = new ArrayList<>();
                commpdityContentIdList = new ArrayList<>();
                favoriteCountList = new ArrayList<>();
                isFavoriteList = new ArrayList<>();
                for (MyPageFavoriteThematicItem.DataBean.ItemsBean j : items) {
                    MyPageFavoriteThematicItem.DataBean.ItemsBean.TopicBean i = j.getTopic();
                    if (i == null) {
                        continue;
                    }
                    titleList.add(i.getTitle());
                    imageUrlList.add(i.getImageUrl());
                    aboutList.add(i.getSummary());
                    detailUrlList.add(i.getDetailUrl());
                    commpdityContentIdList.add(i.getContentId());
                    favoriteCountList.add(i.getFavoriteCount());
                    isFavoriteList.add(i.isFavorited());
                }
                //更新界面&构建List
                Message message = new Message();
                message.what = THEMATIC_OK;
                handler.sendMessage(message);
            }

            @Override
            public void onFailure(Call<MyPageFavoriteThematicItem> call, Throwable t) {

            }
        });
    }

    private void conectCommonityNetWork() {
        //商品收藏部分api
        Retrofit retrofit = CommonRetrofit.getRetrofit();
        MyPageFavoriteCommonityService myPageFavoriteCommonityService = retrofit.create(MyPageFavoriteCommonityService.class);
        Call<MyPageFavoriteCommonityItem> commonityItemCall = myPageFavoriteCommonityService.getItem(2);
        commonityItemCall.enqueue(new Callback<MyPageFavoriteCommonityItem>() {
            @Override
            public void onResponse(Call<MyPageFavoriteCommonityItem> call, Response<MyPageFavoriteCommonityItem> response) {
                List<MyPageFavoriteCommonityItem.DataBean.ItemsBean> items = response.body().getData().getItems();
                commonityName = new ArrayList<>();
                commonityFrom = new ArrayList<>();
                commonityDetilUrl = new ArrayList<>();
                commonityImageUrl = new ArrayList<>();
                commonityIsLike = new ArrayList<>();
                commonityId = new ArrayList<>();
                for (MyPageFavoriteCommonityItem.DataBean.ItemsBean i : items) {
                    MyPageFavoriteCommonityItem.DataBean.ItemsBean.ProductBean product = i.getProduct();
                    commonityName.add(product.getName());
                    commonityFrom.add("淘宝");
                    commonityDetilUrl.add(product.getDetailUrl());
                    commonityImageUrl.add(product.getImageUrl());
                    commonityIsLike.add(true);
                    commonityId.add(product.getProductId());
                }
                Message message = new Message();
                message.what = COMMODITY_OK;
                handler.sendMessage(message);
            }

            @Override
            public void onFailure(Call<MyPageFavoriteCommonityItem> call, Throwable t) {

            }
        });
    }

    private void thematicInit() {
        thematic_items = new ArrayList<>();
        for (int i = 0; i < titleList.size(); i++) {
            Subscribe_item subscribe_item = new Subscribe_item(this);
            subscribe_item.setData(titleList.get(i), aboutList.get(i), imageUrlList.get(i), detailUrlList.get(i), favoriteCountList.get(i), isFavoriteList.get(i), commpdityContentIdList.get(i));
            thematic_items.add(subscribe_item);
        }
        SubscribeListAdapter adapter = new SubscribeListAdapter(this, R.layout.category_subscribe_item, thematic_items);
        ListView listView = new ListView(this);
        listView.setAdapter(adapter);
        viewList.add(listView);
        finishPage++;
    }

    private void commodityInit() {
        RecyclerView commodityView = new RecyclerView(this);
        CommodityRecyclerAdapter adapter = new CommodityRecyclerAdapter(this, commonityName, commonityImageUrl, commonityFrom, commonityDetilUrl, commonityIsLike, commonityId);
        commodityView.setLayoutManager(new GridLayoutManager(this, 3));
        commodityView.setAdapter(adapter);
        viewList.add(commodityView);
        finishPage++;
    }

    private void pageFinish() {
        FavoritesPagerAdapter adapter = new FavoritesPagerAdapter(viewList);
        adapter.notifyDataSetChanged();
        viewPager.setAdapter(adapter);
        tabLayout.addTab(tabLayout.newTab().setText("收藏"));
        tabLayout.addTab(tabLayout.newTab().setText("商品"));
        tabLayout.setupWithViewPager(viewPager);
    }
}
